home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / castools.zip / NEWPHONE.C < prev    next >
Text File  |  1990-01-02  |  3KB  |  107 lines

  1.  
  2.  
  3. /*
  4.    NEWPHONE.C  Function PbCreatePhonebook:  creates a new phonebook file.
  5.  
  6.    This function creates a phonebook file according to the format described in
  7.    the CAS document.
  8.  
  9.    INPUT:  Name to give new phonebook file, and optionally a pointer to a
  10.    phonebook structure.
  11.  
  12.    OUTPUT: If a phonebook structure was provided on input, it is filled with
  13.    the new phonebook's data.
  14. */
  15.  
  16. #include <stdlib.h>
  17. #include <malloc.h>
  18. #include <stdio.h>
  19. #include <memory.h>
  20. #include <sys\types.h>
  21. #include <sys\stat.h>
  22. #include <io.h>
  23. #include <phonebk.h>
  24. #include <pbdflt.h>
  25.  
  26. PB * pascal PbCreatePhonebook(PB *pb,
  27.                               char *name)
  28. {
  29.   PB *NewPb;
  30.   struct stat filestat;   /* for call to stat(), in case file already exists */
  31.   int writ,               /* for return from fwrite() */
  32.       result;             /* for return from stat() */
  33.   long *dummy;
  34.  
  35.   Pberrno = 0;            /* Initially, always reset */
  36.  
  37.   /* First, check params */
  38.   if (name == NULL) {
  39.     Pberrno = INVALIDPARAMETER;
  40.     return(NULL);
  41.   }
  42.  
  43.   /* Check for any file by the same name as the proposed new phonebook */
  44.   result = stat(name, &filestat);
  45.   if (!result) {
  46.     Pberrno = FILEALREADYEXISTS;
  47.     return(NULL);
  48.   }
  49.  
  50.   /* If a phonebook structure provided, use it; otherwise allocate one */
  51.   if (!pb) {
  52.     NewPb = (PB *)calloc(1, sizeof(PB));
  53.     if (!NewPb) {
  54.       Pberrno = OUTOFMEM;
  55.       return(NULL);
  56.     }
  57.   }
  58.   else {
  59.     NewPb = pb;
  60.   }
  61.  
  62.   /* Copy the default header into the new phonebook's header field */
  63.   memcpy(&NewPb->header, &DefaultPbHeader, sizeof(PBH));
  64.  
  65.   /* Create and open for reading and writing the new phonebook file */
  66.   NewPb->fp = fopen(name, "w+b");
  67.   if (!NewPb->fp) {
  68.     Pberrno = CANTOPEN;
  69.     if (!pb) {
  70.       free(NewPb);
  71.     }
  72.     return(NULL);
  73.   }
  74.  
  75.   /* Write the new phonebook's header and offsets array to the file */
  76.   writ = fwrite(&NewPb->header, sizeof(PBH), 1, NewPb->fp);
  77.   if (writ != 1) {
  78.     Pberrno = CANTWRITE;
  79.     fclose(NewPb->fp);
  80.     if (!pb) {
  81.       free(NewPb);
  82.     }
  83.     return(NULL);
  84.   }
  85.   dummy = (long *)calloc(1000, sizeof(long));
  86.   if (!dummy) {
  87.     Pberrno = OUTOFMEM;
  88.     fclose(NewPb->fp);
  89.     if (!pb) {
  90.       free(NewPb);
  91.     }
  92.     return(NULL);
  93.   }
  94.   writ = fwrite(dummy, sizeof(long), 1000, NewPb->fp);
  95.   free(dummy);
  96.   if (writ != 1000) {
  97.     Pberrno = CANTWRITE;
  98.     fclose(NewPb->fp);
  99.     if (!pb) {
  100.       free(NewPb);
  101.     }
  102.     return(NULL);
  103.   }
  104.  
  105.   return(NewPb);    /* And that's all! */
  106. }
  107.